package com.xrh.core.generater.bean;

import com.xrh.core.generater.utils.RegexUtils;

/**
 * @author ronghui.xiao
 * @function 字段信息
 * @date 2017年6月24日
 */
public class ColumnBean {

    //字段名称
    private String columnName;

    //java属性名
    private String propertyName;

    //长度
    private int length;

    //注释
    private String comment;

    //类型
    private String type;

    //java类型
    private String javaType;

    //java类型名
    private String javaTypeName;

    //表信息引用
    private TableInfo tableInfo;

    //是否可以为空 ：YES/NO
    private String nullable;

    //oracle 的小数点
    private int dataScale = 0;

    public ColumnBean() {

    }

    public ColumnBean(TableInfo tableInfo, String columnName, int length, String comment, String type) {
        this(tableInfo, columnName, length, comment, type, 0);
    }


    public ColumnBean(TableInfo tableInfo, String columnName, int length, String comment, String type, int dataScale) {

        this.tableInfo = tableInfo;
        this.columnName = columnName;
        this.propertyName = RegexUtils.generatePropertyName(columnName);
        this.length = length;
        this.comment = comment;
        this.type = type;

        //设置java类型和类型名
        if ("DECIMAL".equals(type) || "DOUBLE".equals(type) || ("NUMBER".equals(type) && dataScale > 0)) {
            this.javaType = "java.lang.Double";
            this.javaTypeName = "Double";
            this.type = "DECIMAL";
        } else if ("BIGINT".equals(type)) {
            this.javaType = "java.lang.Long";
            this.javaTypeName = "Long";
            this.type = "BIGINT";
        } else if ("INTEGER".equals(type) || "INT".equals(type) || "TINYINT".equals(type) || "SMALLINT".equals(type) || "BIGINT".equals(type) || ("NUMBER".equals(type) && dataScale == 0)) {
            this.javaType = "java.lang.Integer";
            this.javaTypeName = "Integer";
            this.type = "INT";
        } else if ("VARCHAR".equals(type) || "VARCHAR2".equals(type)) {
            this.javaType = "java.lang.String";
            this.javaTypeName = "String";
        } else if ("DATETIME".equals(type) || "TIMESTAMP".equals(type) || "DATE".equals(type)) {
            this.javaType = "java.util.Date";
            this.javaTypeName = "Date";
        }
        //特殊处理
        else if ("CHAR".equals(type) && length == 1 && propertyName.equals("isDeleted")) {
            this.javaType = "java.lang.Boolean";
            this.javaTypeName = "Boolean";
        } else {
            this.javaType = "java.lang.String";
            this.javaTypeName = "String";
        }

    }

    public String getColumnName() {
        return columnName;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public String getComment() {
        return comment == null ? "" : comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getJavaType() {
        return javaType;
    }

    public void setJavaType(String javaType) {
        this.javaType = javaType;
    }

    public String getJavaTypeName() {
        return javaTypeName;
    }

    public void setJavaTypeName(String javaTypeName) {
        this.javaTypeName = javaTypeName;
    }

    public boolean isDouble() {
        return "Double".equals(javaTypeName);
    }

    public boolean isInteger() {
        return "Integer".equals(javaTypeName);
    }

    public boolean isString() {
        return "String".equals(javaTypeName);
    }

    public boolean isDate() {
        return "Date".equals(javaTypeName);
    }

    public boolean isBoolean() {
        return "Boolean".equals(javaTypeName);
    }

    public String getPropertyName() {
        return propertyName;
    }

    //当前列是否是主键
    public boolean isPrimaryKey() {
        return propertyName != null && propertyName.equals(tableInfo.getFirstPrimaryKeyPropertyName());
    }

    public TableInfo getTableInfo() {
        return tableInfo;
    }

    public void setTableInfo(TableInfo tableInfo) {
        this.tableInfo = tableInfo;
    }

    public int getDataScale() {
        return dataScale;
    }

    public void setDataScale(int dataScale) {
        this.dataScale = dataScale;
    }

    public String getNullable() {
        return nullable;
    }

    public void setNullable(String nullable) {
        this.nullable = nullable;
    }
}
